package com.ruanyun.util;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 * Created by Administrator on 2016/1/20.
 */
public class RandomStringUtils {
    /** 纯数字 */
    public static final int ALL_NUMBER = 1;
    /** 纯小写字母 */
    public static final int ALL_LOWERCASE = 2;
    /** 纯大写字母 */
    public static final int ALL_CAPITAL = 3;
    /** 纯字母 */
    public static final int ALL_LETTER = 4;
    /** 字母数字混合 */
    public static final int LETTER_NUMBER = 7;

    private static Random RANDOM = new Random();

    public static String randomStr(int type,int length) {
        String resultStr = null;
        switch (type) {
            case ALL_NUMBER:
                resultStr = randomStr("1234567890",length);
                break;
            case ALL_LOWERCASE:
                resultStr = randomStr("qwertyuiopasdfghjklzxcvbnm",length);
                break;
            case ALL_CAPITAL:
                resultStr = randomStr("QWERTYUIOPASDFGHJKLZXCVBNM",length);
                break;
            case ALL_LETTER:
                resultStr = randomStr("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM",length);
                break;
            case LETTER_NUMBER:
            default:
                resultStr = randomStr("1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM",length);
        }
        return resultStr;
    }

    public static String randomStr(String randString,int length) {
        if(length < 0 || randString == null) {
            return null;
        }
        String resultStr = "";
        int len = randString.length();
        for(int i=0;i<length;++i) {
            resultStr += randString.charAt(RANDOM.nextInt(len));
        }
        return resultStr;
    }

    /**
     * 获取随机6位数
     * @return
     */
    public static String randomStr(){
        //随机6位数
        String sb = RandomStringUtils.randomStr(RandomStringUtils.ALL_NUMBER, 6);
        StringBuffer s = new StringBuffer(sb);
        //如果生成数第一位是0要换成别的数
        if (s.substring(0, 1).equals("0")) {
            s.replace(0, 1, "1");
        }
        String code = s.toString();
        return code;
    }

    /**

     * 计算两个日期之间相差的天数

     * @param smdate 较小的时间

     * @param bdate 较大的时间

     * @return 相差天数

     * @throws ParseException

     */

    public static int daysBetween(Date smdate, Date bdate) {

        Calendar cal = Calendar.getInstance();

        cal.setTime(smdate);

        long time1 = cal.getTimeInMillis();

        cal.setTime(bdate);

        long time2 = cal.getTimeInMillis();

        long between_days=(time2-time1)/(1000*3600*24);

        return Integer.parseInt(String.valueOf(between_days));

    }

}
